package com.pocketmenu.persistence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.pocketmenu.business.Constants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ReservationDataSource {
	
	// Database fields
	  private SQLiteDatabase database;
	  private SQLiteHelper dbHelper;
	  private String[] allColumns = 
		  { SQLiteHelper.COLUMN_ID,
			SQLiteHelper.COLUMN_RESERVATION_DATETIME,
			SQLiteHelper.COLUMN_RESERVATION_NUMBER_SEATS,			
			SQLiteHelper.COLUMN_RESERVATION_NOTE,
			SQLiteHelper.COLUMN_RESERVATION_ID_RESTAURANT};

	  public ReservationDataSource(Context context) {
	    dbHelper = new SQLiteHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public Reservation create(Date datetime, int numberSeats, String note, int idRestaurant) {
	    ContentValues values = new ContentValues();
	    
	    SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATETIME_FORMAT);	    
	    values.put(SQLiteHelper.COLUMN_RESERVATION_DATETIME, dateFormat.format(datetime));
	    
	    values.put(SQLiteHelper.COLUMN_RESERVATION_NUMBER_SEATS, numberSeats);
	    values.put(SQLiteHelper.COLUMN_RESERVATION_NOTE, note);
	    values.put(SQLiteHelper.COLUMN_RESERVATION_ID_RESTAURANT, idRestaurant);	    
	    
	    long insertId = database.insert(SQLiteHelper.TABLE_RESERVATION, null,
	        values);
	    
	    Cursor cursor = database.query(SQLiteHelper.TABLE_RESERVATION,
	        allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Reservation newTemp = cursorToObject(cursor);
	    cursor.close();
	    return newTemp;
	  }

	  public void delete(Reservation toDelete) {
	    int id = toDelete.getId();
	    System.out.println("Reservation deleted with id: " + id);
	    database.delete(SQLiteHelper.TABLE_RESERVATION, SQLiteHelper.COLUMN_ID
	        + " = " + id, null);
	  }

	  public List<Reservation> getAll() {
	    List<Reservation> listTemp = new ArrayList<Reservation>();

	    Cursor cursor = database.query(SQLiteHelper.TABLE_RESERVATION,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Reservation temp = cursorToObject(cursor);
	      listTemp.add(temp);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return listTemp;
	  }

	  private Reservation cursorToObject(Cursor cursor) {
	    Reservation temp = new Reservation();
	    temp.setId(cursor.getInt(0));
	    SimpleDateFormat format = new SimpleDateFormat(Constants.DATETIME_FORMAT);
	    Date datetime;
		try {
			datetime = format.parse(cursor.getString(1));
			temp.setDatetime(datetime);
		} catch (ParseException e) {				
			System.out.print("Error getting date from database [ReservationDataSource:cursorToObject]");
		}	
	    temp.setNumberSeats(cursor.getInt(2));
	    temp.setNote(cursor.getString(3));
	    temp.setIdRestaurant(cursor.getInt(4));	    
	    return temp;
	  }
}
